package com.mock.action.organisation.department;

import java.util.ArrayList;
import java.util.List;

import com.mock.dao.BusinessTypeDao;
import com.mock.dao.ContactDao;
import com.mock.dao.CountryDao;
import com.mock.dao.DepartmentDao;
import com.mock.dao.DirectorateDao;
import com.mock.dao.OrganisationDao;
import com.mock.dao.impl.BusinessTypeDaoImpl;
import com.mock.dao.impl.ContactDaoImpl;
import com.mock.dao.impl.CountryDaoImpl;
import com.mock.dao.impl.DepartmentImpl;
import com.mock.dao.impl.DirectorateDaoImp;
import com.mock.dao.impl.OrganisationDaoImpl;
import com.mock.model.BusinessType;
import com.mock.model.Contact;
import com.mock.model.Country;
import com.mock.model.Department;
import com.mock.model.Directorate;
import com.mock.model.Organisation;
import com.opensymphony.xwork2.ActionSupport;

public class AddDepartmentAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DepartmentDao departmentDao = new DepartmentImpl();
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if(directorateId == null) return ERROR;
		directorateIdHidden = directorateId;
		return SUCCESS;
	}
	
	public String getDataFromParent(){
		Directorate directorate;
		DirectorateDao directorateDao = new DirectorateDaoImp();
		try {
			directorate = directorateDao.getById(directorateIdHidden);
			copyFromParent = new ArrayList<String>();
			copyFromParent.add(directorate.getAddressName1());
			copyFromParent.add(directorate.getAddressName2());
			copyFromParent.add(directorate.getAddressName3());
			copyFromParent.add(directorate.getPostcode());
			copyFromParent.add(directorate.getTown());
			copyFromParent.add(directorate.getCounty());
			copyFromParent.add(directorate.getCountry().getCountryId()+"");
			
		} catch (Exception e) { 
			System.out.println("Error Id Directoreate : "+ e.getMessage());
		}		
		return SUCCESS;
	}
	
	public String getDataFromOrganisatio(){
		DirectorateDao directorateDao = new DirectorateDaoImp();
		OrganisationDao organisationDao = new OrganisationDaoImpl();
		try {
			Long idOrganisation = directorateDao.getById(directorateIdHidden).getOrganisation().getOrganisationId();
			Organisation organisation = organisationDao.getById(idOrganisation);
			
			copyFromParent = new ArrayList<String>();
			copyFromParent.add(organisation.getAddressLine1());
			copyFromParent.add(organisation.getAddressLine2());
			copyFromParent.add(organisation.getAddressLine3());
			copyFromParent.add(organisation.getPostcode());
			copyFromParent.add(organisation.getCity());
			copyFromParent.add(organisation.getCounty());
			copyFromParent.add(organisation.getCountry().getCountryId()+"");
			
		} catch (Exception e) { 
			System.out.println("Error Id Directoreate : "+ e.getMessage());
		}		
		return SUCCESS;
	}
	
	public String save(){	
		Department department = new Department();

		Contact contact = null;
		Country country = null;
		BusinessType bt = null;
		Directorate directorate = null;
		if (name == null || name.length() == 0 ) return SUCCESS;
		
		ContactDao contactDao = new ContactDaoImpl();
		if (leadContactId != null)
			try {
				contact = contactDao.getById(leadContactId);
			} catch (Exception e) {
				contact = null;
				System.out.println("Error AddDepartment Contact: "+ e.getMessage());
			}
		CountryDao countryDao = new CountryDaoImpl();
		if (countryId != -1)
			try {
				country = countryDao.getById(countryId);
			} catch (Exception e) {
				country = null;
				System.out.println("Error AddDepartment Country: "+ e.getMessage());
			}
		BusinessTypeDao businessTypeDao = new BusinessTypeDaoImpl();
		if (businessIdHidden != null) {
			try {
				bt = businessTypeDao.getById(businessIdHidden);
			} catch (Exception e) {
				bt = null;
				System.out.println("Error AddDepartment BusinessType: "+ e.getMessage());
			}
		}
		
		DirectorateDao directorateDao = new DirectorateDaoImp();
		if (directorateId != null) {
			try {
				directorate = directorateDao.getById(directorateId);
			} catch (Exception e) {
				directorate = null;
				System.out.println("Error AddDepartment Directorate: "+ e.getMessage());
			}
		}
		
		department.setDepartmentName(name);
		department.setDescription(shortDescription);
		department.setLeadContact(contact);
		department.setAddress1(addressLine1);
		department.setAddress2(addressLine2);
		department.setAddress3(addressLine3);
		department.setPostcode(postcode);
		department.setTown(town);
		department.setCounty(county);
		department.setCountry(country);
		department.setBusinessType(bt);
		department.setFullDescription(fullDescription);
		department.setPhoneNumber(phonenumber);
		department.setFax(fax);
		department.setEmail(email);
		department.setWebAddress(webAddress);
		department.setActive(true);
		department.setDirectorate(directorate);
		
		departmentDao.add(department);
		return SUCCESS;
	}

	
	
	public String getAllName(){
		List<Department> departs = departmentDao.getAll(Department.class);
		
		for (Department department : departs) {
			listName.add(department.getDepartmentName());
		}
		return SUCCESS;
	}
	
	List<String> listName = new ArrayList<String>() ;
	List<Department> departments = new ArrayList<Department>();
	private List<Country> countries = new ArrayList<Country>();
	private Long directorateId ;
	public static Long directorateIdHidden=0L;
	private String name;
	private String shortDescription;
	private Long leadContactId;
	private String addressLine1;
	private String addressLine2;
	private String addressLine3;
	private String postcode;
	private String town;
	private String county;
	private Long countryId;
	private Long businessIdHidden;
	private String fullDescription;
	private String phonenumber;
	private String fax;
	private String email;
	private String webAddress;
	
	
	private List<String> copyFromParent;

	public List<String> getListName() {
		return listName;
	}

	public void setListName(List<String> listName) {
		this.listName = listName;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Country> getCountries() {
		CountryDao dao = new CountryDaoImpl();
		countries = dao.getAll(Country.class);
		return countries;
	}

	public void setCountries(List<Country> countries) {
		this.countries = countries;
	}

	public Long getDirectorateId() {
		return directorateId;
	}

	public void setDirectorateId(Long directorateId) {
		this.directorateId = directorateId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getShortDescription() {
		return shortDescription;
	}

	public void setShortDescription(String shortDescription) {
		this.shortDescription = shortDescription;
	}

	public Long getLeadContactId() {
		return leadContactId;
	}

	public void setLeadContactId(Long leadContactId) {
		this.leadContactId = leadContactId;
	}

	public String getAddressLine1() {
		return addressLine1;
	}

	public void setAddressLine1(String addressLine1) {
		this.addressLine1 = addressLine1;
	}

	public String getAddressLine2() {
		return addressLine2;
	}

	public void setAddressLine2(String addressLine2) {
		this.addressLine2 = addressLine2;
	}

	public String getAddressLine3() {
		return addressLine3;
	}

	public void setAddressLine3(String addressLine3) {
		this.addressLine3 = addressLine3;
	}


	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getTown() {
		return town;
	}

	public void setTown(String town) {
		this.town = town;
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public Long getCountryId() {
		return countryId;
	}

	public void setCountryId(Long countryId) {
		this.countryId = countryId;
	}

	public Long getBusinessIdHidden() {
		return businessIdHidden;
	}

	public void setBusinessIdHidden(Long businessIdHidden) {
		this.businessIdHidden = businessIdHidden;
	}

	public String getFullDescription() {
		return fullDescription;
	}

	public void setFullDescription(String fullDescription) {
		this.fullDescription = fullDescription;
	}

	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWebAddress() {
		return webAddress;
	}

	public void setWebAddress(String webAddress) {
		this.webAddress = webAddress;
	}

	public List<String> getCopyFromParent() {
		return copyFromParent;
	}

	public void setCopyFromParent(List<String> copyFromParent) {
		this.copyFromParent = copyFromParent;
	}

}
